#ifndef TEXTBOX_H_
#define TEXTBOX_H_

#define MAX_SIZE 15

//Space between first character rendered and border of textbox,
//values over or equal to 4 recommended.
#define MARGIN 4
#define CURSOR_BLINK_RATE 3

#include "../../common/utils/Logger.h"

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "../WidgetClickeableWithText.h"
#include "../Timer.h"
#include <string>
#include <iostream>


class Textbox: public WidgetClickeableWithText {
public:
	Textbox(std::string defText,int posX, int posY, Widget *parent);
	Textbox(std::string defText,int posX, int posY, int width, int height, Widget *parent);

	void setFocus();
	void unsetFocus();
	bool getFocus() const;
	void cleanInput();
	std::string getInput();

	virtual void handleEvent(Event event);

	virtual void resolve();

	virtual void showOn(SDL_Surface* surface);

	virtual ~Textbox();

private:

	bool inputChanged;
	bool selected;
	bool enable;
	bool cursorMoved;

	Sint16 screenPosCursor;
	size_t stringPosCursor;
	int yMargin;

	Timer cursorTimer;

	bool mouseLeftBtnPressed;
	bool mouseIsOver;
	bool keyDown;
	bool isChar;
	bool backSpaceKey;
	bool deleteKey;
	bool leftKey;
	bool rightKey;
	char tempInput;

	Uint8 keyState;
	bool keyPressed;

	SDL_Rect widArea;  // Rendered copy of widget area
	SDL_Rect textSrcArea;
	SDL_Rect textDstArea;

    TTF_Font* font;

    SDL_Surface* renderedText;

	void drawBox(SDL_Surface* s);
	void drawText(SDL_Surface* s);
	void drawCursor(SDL_Surface* s);
	int getTextWidth(const std::string& txt);
	void renderText();
	void reCalculateCursorPosition();

	void resizeFontToTextArea();

};

#endif /* TEXTBOX_H_ */
